package com.dingwen.treasure.business.feign.fallback;

import cn.hutool.core.util.StrUtil;
import com.dingwen.treasure.base.constant.ServerConstant;
import com.dingwen.treasure.base.pojo.vo.Result;
import com.dingwen.treasure.base.pojo.vo.ResultGenerator;
import com.dingwen.treasure.business.feign.TreasureTaskFeignClient;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * open feign调用: business --> task 的 hystrix 兜底方法
 *
 * @author dingwen
 * @date 2022/03/31
 */
@Slf4j
@Component
public class TreasureTaskFeignClientFallback implements FallbackFactory<TreasureTaskFeignClient> {

    /**
     * throwable
     */
    private Throwable throwable;


    /**
     * task feign 调用客户端： business --> task
     */
    private final TreasureTaskFeignClient treasureTaskFeignClient = new TreasureTaskFeignClient() {
        @Override
        public Result<Long> test(Long id) {
            String message = StrUtil.format("{}通过feign调用{},出现错误，方法{},入参{},异常信息{}", ServerConstant.TREASURE_BUSINESS, ServerConstant.TREASURE_TASK_QUARTZ, "test", id, throwable.getMessage());
            log.error(message);
            return ResultGenerator.genFailureResult(message);
        }
    };

    @Override
    public TreasureTaskFeignClient create(Throwable throwable) {
        this.throwable = throwable;
        return treasureTaskFeignClient;
    }
}
